/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * panelCrearUsuario.java
 *
 * Created on Apr 8, 2011, 8:36:14 PM
 */

package frontera.interfaz.panel;

import control.SIRHApp;
import control.comparaciones.Comparacion;
import control.comportamiento.ComportamientoListaBusqueda;
import control.comportamiento.EscucharBusqueda;
import control.filtros.Filtro;
import entidad.Empleado;
import entidad.Usuario;
import frontera.interfaz.listRenderer.ListRendererEmpleados;
import frontera.interfaz.panel.herramientas.Busqueda;
import frontera.interfaz.panel.herramientas.PanelHerramientasBuscar;
import frontera.interfaz.panel.resultados.PanelResultadosLista;
import java.util.ArrayList;

/**
 *
 * @author tuareg
 */
public class PanelCrearUsuario extends javax.swing.JPanel implements EscucharBusqueda<Empleado> {

    /** Creates new form panelCrearUsuario */
    public PanelCrearUsuario() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        panelDatosBasicos = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        labelNombre = new javax.swing.JLabel();
        nombreTF = new javax.swing.JFormattedTextField();
        jPanel6 = new javax.swing.JPanel();
        fieldEmpleado = new javax.swing.JTextField();
        labelDepartamento = new javax.swing.JLabel();
        buscarCargoB = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        labelSalarioBasico = new javax.swing.JLabel();
        claveTF = new javax.swing.JPasswordField();
        jPanel4 = new javax.swing.JPanel();
        labelSalarioBasico1 = new javax.swing.JLabel();
        claveVerTF = new javax.swing.JPasswordField();
        botonCancelar = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        labelResultado = new javax.swing.JLabel();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/AddUsuario.png"))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 32));
        jLabel2.setText("Creando Nuevo Usuario");

        panelDatosBasicos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Básicos"));
        panelDatosBasicos.setLayout(new java.awt.GridLayout(0, 1));

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        labelNombre.setText("Nombre");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombre)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1.add(jPanel5);

        fieldEmpleado.setEditable(false);
        fieldEmpleado.setText("Sin Asignar");
        fieldEmpleado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldEmpleadoActionPerformed(evt);
            }
        });

        labelDepartamento.setText("Empleado Asociado");

        buscarCargoB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/search.png"))); // NOI18N
        buscarCargoB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarCargoBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelDepartamento)
                .addGap(18, 18, 18)
                .addComponent(fieldEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buscarCargoB, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buscarCargoB, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelDepartamento)
                        .addComponent(fieldEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel1.add(jPanel6);

        panelDatosBasicos.add(jPanel1);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        labelSalarioBasico.setText("Contraseña");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelSalarioBasico)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(claveTF, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSalarioBasico)
                    .addComponent(claveTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.add(jPanel3);

        labelSalarioBasico1.setText("Verificar Contraseña");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelSalarioBasico1)
                .addGap(18, 18, 18)
                .addComponent(claveVerTF, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSalarioBasico1)
                    .addComponent(claveVerTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.add(jPanel4);

        panelDatosBasicos.add(jPanel2);

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Save.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(panelDatosBasicos, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(labelResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(428, Short.MAX_VALUE)
                        .addComponent(botonGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(botonCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jLabel2)))
                .addGap(11, 11, 11)
                .addComponent(panelDatosBasicos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(labelResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCancelar)
                    .addComponent(botonGuardar))
                .addContainerGap(161, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        SIRHApp.getInstance().getVentanaPrincipal().setPanelUsuariosPrincipal(SIRHApp.getInstance().getVentanaPrincipal().getPanelUsuarios());
        ((PanelHerramientasBuscar<Usuario>) SIRHApp.getInstance().getFactoryInterfaz().getPanelUsuarios().getPanelHerramientas()).inicializarBusqueda();
}//GEN-LAST:event_botonCancelarActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed

        String resultadoOperacion = SIRHApp.getInstance().getAdmUsuarios().crearUsuario(nombreTF.getText(),
                String.valueOf(claveTF.getPassword()),
                empleadoAsociado);

        if ( String.valueOf(claveTF.getPassword()).equals(String.valueOf(claveVerTF.getPassword()))){

            if(resultadoOperacion.equals("")){
                SIRHApp.getInstance().getVentanaPrincipal().getPanelUsuarios().actualizarLista(SIRHApp.getInstance().getSistema().getUsuarios());
                SIRHApp.getInstance().getVentanaPrincipal().setPanelUsuariosPrincipal( SIRHApp.getInstance().getVentanaPrincipal().getPanelUsuarios());
                ((PanelHerramientasBuscar<Usuario>) SIRHApp.getInstance().getFactoryInterfaz().getPanelUsuarios().getPanelHerramientas()).inicializarBusqueda();
            }
            else{
                    labelResultado.setText(resultadoOperacion);
            }
        }

        else{
                labelResultado.setText("Las contraseñas no coinciden. Por favor verifique que haya digitado correctamente.");
        }
 
}//GEN-LAST:event_botonGuardarActionPerformed

    private void fieldEmpleadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldEmpleadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fieldEmpleadoActionPerformed

    private void buscarCargoBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarCargoBActionPerformed
        PanelBusqueda<Empleado> panelBuscarEmpleados =
                new PanelBusqueda<Empleado>(
                    new PanelResultadosLista<Empleado>(
                        SIRHApp.getInstance().getSistema().getEmpleados(),
                        new ListRendererEmpleados()
                    ),
                    new ComportamientoListaBusqueda<Empleado>(this)
        );
        panelBuscarEmpleados.setPanelHerramientas(
                new PanelHerramientasBuscar<Empleado>(
                    panelBuscarEmpleados, SIRHApp.getInstance().getFiltrosEmpleados(),
                    new Busqueda() {

                        public ArrayList buscar(Filtro tipoFiltro, Comparacion comparacion, String filtro) {
                            return SIRHApp.getInstance().getAdmEmpleados().buscar(tipoFiltro, comparacion, filtro);

                        }
                    }
        ));
        SIRHApp.getInstance().getVentanaPrincipal().setPanelUsuariosPrincipal
              (panelBuscarEmpleados);
    }//GEN-LAST:event_buscarCargoBActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton buscarCargoB;
    private javax.swing.JPasswordField claveTF;
    private javax.swing.JPasswordField claveVerTF;
    private javax.swing.JTextField fieldEmpleado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel labelDepartamento;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelResultado;
    private javax.swing.JLabel labelSalarioBasico;
    private javax.swing.JLabel labelSalarioBasico1;
    private javax.swing.JFormattedTextField nombreTF;
    private javax.swing.JPanel panelDatosBasicos;
    // End of variables declaration//GEN-END:variables


    public void procesarResultadoBusqueda(Empleado resultado) {
        empleadoAsociado = resultado;
        fieldEmpleado.setText(resultado.getNombre());
        SIRHApp.getInstance().getVentanaPrincipal().setPanelUsuariosPrincipal(this);
    }


    private Empleado empleadoAsociado = null;
}
